<template>
  <div class="chart-container">
    <div class="chart-header">
      <h3>人员统计</h3>
      <a-radio-group v-model:value="chartType" size="small" @change="updateChart">
        <a-radio-button value="daily">日视图</a-radio-button>
        <a-radio-button value="weekly">周视图</a-radio-button>
        <a-radio-button value="monthly">月视图</a-radio-button>
      </a-radio-group>
    </div>
    <div ref="chartRef" class="chart"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Object,
    default: () => ({
      daily: {
        xAxis: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
        data: [152, 165, 148, 178, 160, 145, 138]
      },
      weekly: {
        xAxis: ['第一周', '第二周', '第三周', '第四周'],
        data: [755, 820, 890, 780]
      },
      monthly: {
        xAxis: ['1月', '2月', '3月', '4月'],
        data: [3200, 3500, 3800, 3600]
      }
    })
  }
})

const chartRef = ref(null)
const chartType = ref('daily')
let chart = null

const chartOption = {
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    axisLine: {
      lineStyle: {
        color: '#fff'
      }
    }
  },
  yAxis: {
    type: 'value',
    axisLine: {
      lineStyle: {
        color: '#fff'
      }
    },
    splitLine: {
      lineStyle: {
        color: 'rgba(255, 255, 255, 0.1)'
      }
    }
  },
  series: [
    {
      name: '在场人数',
      type: 'bar',
      barWidth: '60%',
      data: [152, 165, 148, 178, 160, 145, 138],
      itemStyle: {
        color: '#409EFF'
      }
    }
  ]
}

const getChartData = () => {
  switch (chartType.value) {
    case 'daily':
      return {
        xAxis: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
        data: [152, 165, 148, 178, 160, 145, 138]
      }
    case 'weekly':
      return {
        xAxis: ['第一周', '第二周', '第三周', '第四周'],
        data: [755, 820, 890, 780]
      }
    case 'monthly':
      return {
        xAxis: ['1月', '2月', '3月', '4月'],
        data: [3200, 3500, 3800, 3600]
      }
  }
}

const updateChart = () => {
  if (!chart) return
  
  const { xAxis: xAxisData, data } = getChartData()
  const option = {
    ...chartOption,
    xAxis: {
      ...chartOption.xAxis,
      data: xAxisData
    },
    series: [{
      ...chartOption.series[0],
      data: data
    }]
  }
  chart.setOption(option)
}

onMounted(() => {
  chart = echarts.init(chartRef.value)
  chart.setOption(chartOption)
  
  // 添加窗口大小变化的监听
  window.addEventListener('resize', handleResize)
})

const handleResize = () => {
  chart?.resize()
}

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  chart?.dispose()
})
</script>

<style scoped>
.chart-container {
  background-color: rgba(255, 255, 255, 0.05);
  background-color: rgb(0 20 53 / 79%);
  border-radius: 6px;
  padding: 1.5vh;
  height: 100%;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.chart-header h3 {
  margin: 0;
  color: #409EFF;
  font-size: 16px;
}

.chart {
  height: calc(100% - 5vh);
  width: 100%;
}

:deep(.ant-radio-button-wrapper) {
  background: transparent !important;
  border-color: rgba(255, 255, 255, 0.1) !important;
  color: #fff !important;
}

:deep(.ant-radio-button-wrapper-checked) {
  background: #409EFF !important;
  border-color: #409EFF !important;
}
</style> 